/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.ConstantOperCode;
import org.grayrabbit.cms.dao.system.OperCodeDao;
import org.grayrabbit.cms.entity.OperCode;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.service.system.OperCodeService;
import org.grayrabbit.cms.vo.system.OperCodeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 15:55:06  
 */
@Service
@Transactional
public class OperCodeServiceImpl extends GenericServiceImpl implements OperCodeService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("operCodeDaoImpl")
	private OperCodeDao operCodeDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see OperCodeService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public OperCode getOperCode(final String operId){
		OperCode operCode = operCodeDao.findById(operId);
		operCodeDao.initialize(operCode);
		return operCode;
	}
	
	/**
	 * @see OperCodeService#delete(String)
	 */
	@Transactional
	public void delete(final OperCode operCode) {
		operCodeDao.makeTransient(operCode);
	}
	
	/**
	 * @see OperCodeService#save(OperLog)
	 */
	@Transactional
	public void save(final OperCode operCode) {
		operCodeDao.makePersistent(operCode);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see OperCodeService#getPage(OperCodeBean)
	 */
	@Transactional(readOnly=true)
	public Page<OperCode> getPage(final OperCodeBean operCodeBean){
		Page<OperCode> page =  new Page<OperCode>();
		page.setTotal(operCodeDao.count(operCodeBean));
		page.setResult(operCodeDao.findByQc(operCodeDao.assembly(operCodeBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see OperCodeService#list(Page, OperCodeBean)
	 */
	public void list(Page<OperCode> page, OperCodeBean operCodeBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(operCodeDao.count(operCodeBean));
		}
		page.setResult(operCodeDao.findByPage(page,operCodeBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see OperCodeService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			operCodeDao.makeTransient(operCodeDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				operCodeDao.clear();
		}
		
	}
	
	
	/** 初始化权限 
	 * @param list  权限实体list
	 */
	public void bathSaveOperCode(List<OperCode> list){
		if(list == null || list.isEmpty()) return ;
		for(OperCode operCode : list){
			operCodeDao.makePersistent(operCode);
		}
	}
	
	@Transactional(readOnly=true)
    public List<OperCode> findAllCodes(){
    	return operCodeDao.findAllCodeOrderAsc();
    }
	
	  /**
     *  权限码按模块分装 用于页面展示
     * @return
     */
	@Transactional(readOnly=true)
	public List<List<OperCode>> findAllAreaCodes(){		 
    	return loadOperCodes(findAllCodes());
    }
	
	private  List<List<OperCode>> loadOperCodes(List<OperCode> list){
	    	List<List<OperCode>> codeList = new ArrayList<List<OperCode>>();
			List<OperCode> goble = new ArrayList<OperCode>();
			List<OperCode> site = new ArrayList<OperCode>();
			List<OperCode> cat = new ArrayList<OperCode>();
			List<OperCode> topic = new ArrayList<OperCode>();
			List<OperCode> smsc = new ArrayList<OperCode>();
			List<OperCode> oms = new ArrayList<OperCode>();
		 
			codeList.add(goble);
			codeList.add(site);
			codeList.add(cat);
			codeList.add(topic); 
			codeList.add(smsc); 
			codeList.add(oms); 
			
			if(list == null || list.isEmpty())  return codeList;
			for(OperCode c: list){
				if(StringUtils.isNotBlank(c.getOperCode()) && c.getOperCode().startsWith(ConstantOperCode.GOBLE_FLAG)){
					goble.add(c);
				}else if (StringUtils.isNotBlank(c.getOperCode()) && c.getOperCode().startsWith(ConstantOperCode.SITE_FLAG)){
					site.add(c);
				}else if (StringUtils.isNotBlank(c.getOperCode()) && c.getOperCode().startsWith(ConstantOperCode.CAT_FLAG)){
					cat.add(c);
				}else if (StringUtils.isNotBlank(c.getOperCode()) && c.getOperCode().startsWith(ConstantOperCode.TOPIC_FLAG)){
					topic.add(c);
				}else if(StringUtils.isNotBlank(c.getOperCode()) && c.getOperCode().startsWith(ConstantOperCode.SMSC_FLAG)){
					smsc.add(c);
				}else if(StringUtils.isNotBlank(c.getOperCode()) && c.getOperCode().startsWith(ConstantOperCode.OMS_FLAG)){
					oms.add(c);
				} 
			}
			
			return codeList;
		}
	
	  /**
     * 根据权限码查询
     * @param code
     * @return
     */
    public OperCode findByCode(String code){
    	return operCodeDao.findByCode(code);
    }
	
 
}